<?php
header('Content-Type: application/json; charset=utf-8');
error_reporting(1);

require_once(__DIR__ . "/C5Client.php");

// ob_clean();

$log = "\r\n";
$uri = get_parameter("uri");
$params = $params = get_parameters();

if (empty($uri)) {
    // 读不到参数, 读取整个body部分
    $params = json_decode(file_get_contents('php://input'), true);
}

if (isset($params["uri"])) {
    $uri = $params["uri"];
}


if (!empty($uri)) {

    $client = new C5Client();
    $client->setUrl($configs["api_base_url"] . $uri);

    // unset($params["method"]);
    // unset($params["uri"]);
    $client->setParameters($params);
    $client->call();

    $log .= ("params : \r\n" . $client->buildContent() . "\r\n");
    $log .= ("return : \r\n");

    $log .= ("url : " . $client->getUrl() . "\r\n");
    $string = trim($client->getResult());
    if (!empty($string)) {
        if (substr($string, 0, 1) != "{") {
            // 不是JSON格式的数据，应该出现的异常
            $logs .= "异常数据:\r\n";
            $logs .= $string;
            $logger->error($logs);
        } else {
            $logs .= json_encode(json_decode($string, true), JSON_PRETTY_PRINT);
            $logger->debug($logs);
        }
    } else {
        $logs .= "没有返回内容";
        $logger->debug($logs);
        $string = '{"data" : ""}';
    }


    echo $string;
}

// ob_flush();

